package com.ljl.util;

public class StringSimilarity {
    public  void test3(){

    }
 //动态规划-编辑距离
    public int editDistance(String str1, String str2) {
        int length1 = str1.length();
        int length2 = str2.length();
        int[][] martix = new int[length1 + 1][length2 + 1];
        //初始化边界值(忽略计算时的边界情况)
        for (int i = 0; i <= length1; i++) {
            martix[i][0] = i;
         }
        for (int j = 0; j <= length2; j++) {
            martix[0][j] = j;
        }

        //矩阵的 X 坐标
        for (int i = 1; i <= length1; i++) {
            //矩阵的 Y 坐标
            for (int j = 1; j <= length2; j++) {
                //相等情况
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    martix[i][j] = martix[i - 1][j - 1];
                }  else {
                    //取“左前方”，“上方”，“左方“的最小值
                    int min = Math.min(martix[i - 1][j], martix[i][j - 1]);
                    //获取最小值
                    min = Math.min(min, martix[i - 1][j - 1]);
                    martix[i][j] = min + 1;
                }
            }
         }
         //返回字符串的编辑距离
        return martix[length1][length2];
    }
    public  void test2(){
        System.out.println(2222222);
        System.out.println(66666);
        System.out.println(77777);
        System.out.println(11111);
        System.out.println(333333);
        System.out.println(44444);
        System.out.println(55555555);
        System.out.println(8888888);
    }
    public  void test(){

    }
    public static void main(String[] args) {
        StringSimilarity ss = new StringSimilarity();
        String str1 = "1234b";
        String str2 = "12a34dfertrt";
        int result = ss.editDistance(str1, str2);
        System.out.println(result);
    }
}